//
// Created by denglibin on 2019/6/28 0028.
//

#include <iostream>
#include <string>
#include <sstream>
using namespace std;


//数组的引用
void arr_reference()
{
    int a[10] = {1,2,3,4,5,6,7,8,9,10};
    //一维数组的引用
    int(&ra)[10] = a;
    //int(&ra)[10](a);

    ra[0] = 9;
    cout << a[0] << endl;
    //二维数组的引用
    int b[2][5] = { 1,2,3,4,5,6,7,8,9,10 };

    int(&rb)[2][5] = b;

    for (int i = 0; i < 2; i++)
    {
        for (int j = 0; j < 5; j++)
        {
            ostringstream oss;
            oss << i << "-" << j<<"\t";
            cout << oss.str();
        }
        cout << "\n";
    }
}

//函数指针的引用

int jia(int a, int b)
{
    return a + b;
}
int jian(int a, int b)
{
    return a - b;
}

void test_fun_pointer_reference()
{
    int(*p)(int a, int b) = jia;

    int c = p(1,2);

    cout << c << endl;
    //函数指针的引用
    int(*&rp)(int, int) = p;
    //改变函数指针的指向
    rp = jian;

    int d = p(5,1);

    cout << d << endl;
}

//返回一个函数指针

int(*get_fun_pointer(int a))(int, int)
{
    return jia; //返回该函数
}

//测试返回函数指针
void test_get_fun_reference()
{
    int(*p)(int, int) = get_fun_pointer(23);

    int c = p(1, 4);

    cout << c << endl;
}

//返回函数指针的引用

int(* & get_fun_pointer_reference(int a, int b))(int, int)
{
    int(*rp)(int, int) = jia;

    return rp;
}
//测试返回函数指针的引用
void test_get_fun_pointer_reference()
{
    int(*&rp)(int, int) = get_fun_pointer_reference(1,2);

    int c = rp(3,5);

    cout << c << endl;

    //改变函数指针的指向
    rp = jian;

    int d = rp(3,5);

    cout << d << endl;
}

void main3()
{
    //arr_reference();
    //test_fun_pointer_reference();
    //test_get_fun_reference();

    test_get_fun_pointer_reference();
    system("pause");
}